#KeyEvent.py
#enconding=utf-8

import sys
from PyQt5.QtWidgets import QWidget,QApplication,QLabel
from PyQt5.QtCore import Qt

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300,300,350,250)
        self.setWindowTitle('点豆开花出品')

        self.lab=QLabel('方向',self)

        self.lab.setGeometry(150,100,50,50)

        self.show()

    def keyPressEvent(self,e):
        if e.key() == Qt.Key_Up:
            self.lab.setText('↑')
        elif e.key() == Qt.Key_Down:
            self.lab.setText('↓')
        elif e.key() == Qt.Key_Left:
            self.lab.setText('←')
        elif e.key() == Qt.Key_Right:
            self.lab.setText('→')
        else:
            self.lab.setText('未定义')

if __name__ == '__main__':
    app=QApplication(sys.argv)
    ex=Example()
    sys.exit(app.exec_())
